﻿using System;
using VegetableSale.NET.Dao;
using VegetableSale.NET.Entities;
using VegetableSale.NET.Entities.Good;

namespace VegetableSale.NET.Services
{
    public class UserService
    {
        private UserMapper _userMapper = new UserMapper();

        public Result Login(string username, string password)
        {
            User user = _userMapper.SelectOne(username, password);
            if (user == null)
            {
                return new Result(400, "账号或密码错误", "");
            }
            else
            {
                LoginVo loginVo = new LoginVo(user.Id, Guid.NewGuid().ToString(), user);

                return new Result(200, "登录成功", loginVo);
            }
        }

        public Result getUserList(QueryDTO queryDTO)
        {
            UserPage users = new UserPage();
            uint pageNo = queryDTO.PageNo;
            uint pageSize = queryDTO.PageSize;
            List<User> userList = _userMapper.getUserList(queryDTO);
            List<User> returnUsers = new List<User>();

            if (pageSize * pageNo - 1 >= userList.Count)
            {
                for (uint i = pageSize * (pageNo - 1); i <= userList.Count - 1; i++)
                {
                    User user = userList[(int)i];
                    returnUsers.Add(user);
                }
            }
            else
            {
                for (uint i = pageSize * (pageNo - 1); i <= pageSize * pageNo - 1; i++)
                {
                    User user = userList[(int)i];
                    returnUsers.Add(user);
                }
            }

            users.Records = returnUsers;
            users.Pages = (int)pageNo;
            users.Size = pageSize;
            users.Total = (uint)userList.Count;

            return new Result(200, "搜索成功", users);
        }

        public Result addUser(User user)
        {
            _userMapper.addUser(user);
            return new Result(200, "操作成功", 1);
        }

        public Result userDelete(int userid)
        {
            _userMapper.userDelete(userid);
            return new Result(200, "操作成功", 1);
        }

        public Result userUpdate(User user)
        {
            _userMapper.userUpdate(user);
            return new Result(200, "操作成功", 1);
        }
    }
}

